<template>
  <div class="decrease  ">
    <section class="input-btn flex-c">
      <article class="flex-item">
       
        <ul>
          <li v-for="(item, index) in sreach_data" :key="index" class="flex-c"
            :class="item.active ? 'active' + item.active : ''">
            <h4 v-if="item.name">{{ item.name }}:</h4>
            <h5 :class="index == 0 ?
              parseFloat(item.num) > 100 ? 'active1' : 'active2' :
              parseFloat(item.num) > 0 ? 'active1' : 'active2'" v-if="!item.active || item.active != 1">
              {{ item.num }}
            </h5>
          </li>
       
        </ul>
        <div>
            {{
              analyzeTrend(bug_shell_a_d_a.day_loss_profit,day_list.time)
            }}
          </div>
      </article>
      <ul v-if="bug_shell_a_d_a!=null">
        
        <li v-for="(item, index) in day_list_data" :key="index" class="flex-c">
          <h6 v-if="item.item_data==undefined">({{ item.time }})</h6>   
          <div  class=" table" v-for="(item2, index) in item.item_data">
            <div class="flex-c" >
              <h5 :class="item2.num > 0 ? 'active1' : 'active2'" @click="getzdf_deal_data(item2.time,item2)" >{{ 
                ( bug_shell_a_d_a.day_loss_profit[ timeAt(item2.end_time)]-
                bug_shell_a_d_a.day_loss_profit[  timeAt(item2.start_time)]).toFixed(2)  }}</h5> 
            <span>{{ item2.index }}天</span>
            </div>
            <h6 >({{ item2.time }})</h6>   
          </div>
        </li>
      </ul>

    </section>
      <article>         
          <div class="flex-c table select" v-if="this_zdf_num!=null"  v-for="(item,index) in this_zdf_num">
              <h5 :class="item.num > 0 ? 'active1' : 'active2'">{{
                    (item.num>0?'上涨趋势=':'下跌趋势=') +
                    item.num  }}%  </h5> 
              <span>持续{{ item.index }}天</span>
              <h6>({{ item.time }})</h6>  
          </div>
      </article>
      
      <el-collapse v-model="activeNames">
        <el-collapse-item title="查看详情" name="1">
          <section class="flex-c" style="    background-color: #343434;"> 
            <ul v-if="week_list == null" class="flex-item day">
            
              <li v-for="(item, index) in day_list_data" :key="index" class="flex-c">
                <h4 v-if="item.name">{{ item.name }}:</h4>
    
                <div  class=" table" v-for="(item2, index) in item.item_data">
                  <div class="flex-c" :class="index==0?item.active?'select'+item.active[0]:'':''" >
                    <h5 :class="item2.num > 0 ? 'active1' : 'active2'">{{ item2.num }}%</h5> 
                  <span>{{ item2.index }}天</span>
                  </div>
                  <h6 >({{ item2.time }})</h6>   
                </div>
              </li>
            </ul>
            <div class="box flex-item">
              <ul v-if="week_list == null">
                <div class="flex-c">
                  <el-input class="time" v-model="time_data" placeholder="输入买入日期(xxxx-xx-xx)" size="medium"></el-input>
                  <el-button @click="toSreach(time_data, 0)" size="medium">确定 </el-button>
                </div>
                <li v-for="(item, index) in this_list_data.sz" :key="index" class="flex-c">
                  <h4>{{ item.name }}:</h4>
                  <div class="gird_box">
              
                  <div class="flex-c" >
                      <h5 :class="item.num > 0 ? 'active1' : 'active2'">{{ item.num }}%</h5> 
                    <span>{{ item.index }}天</span>
                  </div>
                  <h6 >({{ item.time }})</h6>   
                </div>
                  <div class="gird_box">
              
                    <div class="flex-c" >
                      <h5 :class="this_list_data.xd[index].num > 0 ? 'active1' : 'active2'">{{ this_list_data.xd[index].num }}%</h5> 
                      <span>{{ this_list_data.xd[index].index }}天</span>
                    </div>
                    <h6 >({{ this_list_data.xd[index].time }})</h6>   
                  </div>
                </li>
              </ul>
            </div>
          </section>
        </el-collapse-item>
      </el-collapse>

   
  </div>
</template>

<script>
import {
  dealFlowData,
  cycleMax,
  getYearlyFluctuation
} from "./decrease.js"
import {
  dealDayList
} from "./decrease_dealDayList.js"
export default {
  data() {
    return {
      activeNames: [],
      week_list_data: [],
      day_list_data: [],
      this_list_data: {sz:[],xd:[]},
      time_data: "",
      sreach_data: [],
      this_zdf_num:null,//当前的值
    }
  },
  created() {
 
   },
  props: {
    // 基金数据
    fund_data: {
      type: Object,
      default: null,
    },
    week_list: {
      type: Object,
      default: null,
    },
    day_list: {
      type: Object,
      default: null,
    },
    //  初始化的值
    dealFlow: {
      type: Array,
      default: null,
    },
    inflowArray: {
      type: Array,
      default: null,
    },
    inflow: {
      type: Array,
      default: null,
    },
    bug_shell_a_d_a: {
      type: Object,
      default: null,
    },
  },
  components: {},
  mounted() {
    if (this.week_list != null) {
      this.week_list_data = this.dealWeekList(this.week_list)
    } else {
      // this.this_list_data = 
      this.dealThisData(this.inflow)
    }
    //搜索计算
    if (this.dealFlow != null) {
      let all_num = this.dealFlow
      if (this.dealFlow.length > 10) {
        all_num = this.dealFlow.reverse()
      }

      this.toSreach(all_num)
    }
  },
  watch: {
    //获取日期的数据
    week_list: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val != null) { }
      },
    },
    day_list: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val != null) {
       //   console.log("dat_list", val)
        }
      },
    },
    inflow: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val != null) { }
      },
    },
    dealFlow: {
      immediate: true,
      deep: true,
      handler(val) {
        if (val != null) {
          let all_num = val
          if (val.length > 10) {
            all_num = val.slice().reverse()
          }
          this.toSreach(all_num)
        }
      },
    },
  },
  methods: {
    findLMax(data) {
        if (!data || data.length === 0) return null;
        // 找到最晚的end_time
        let latestTime = data[0].end_time;
        for (const item of data) {
            if (item.end_time > latestTime) {
                latestTime = item.end_time;
            }
        }

        // 筛选出所有最晚时间的元素
        const latestItems = data.filter(item => item.end_time === latestTime);
    
        // 找出绝对值最大的元素
        let maxItem = latestItems[0];
        let maxAbs = Math.abs(parseFloat(latestItems[0].num));
        for (const item of latestItems) {
            const currentAbs = Math.abs(parseFloat(item.num));
            if (currentAbs >= maxAbs) {
                maxAbs = currentAbs;
                maxItem = item;
            }
        }
        let two_index=  maxItem.num>0?data.indexOf(maxItem)+1:data.indexOf(maxItem)-1
        console.log("latestTime",maxItem,data[two_index])
        // 返回该元素在原数组中的位置和元素本身
        return  [
          maxItem,data[two_index]
        ]
        //maxItem.name+maxItem.num+'%'+maxItem.time
        //  {
        //     index: data.indexOf(maxItem),
        //     element: maxItem
        // };
    },
    analyzeTrend(data,all_time, dp_zhi, tp_zhi) {
        const deal_num=10000
        if (data.length === 0) return {dp_index: -1, tp_index: -1};
        const lastValue = data[data.length - 1];
        const jishu= Math.trunc(lastValue / deal_num) * deal_num;
        if(jishu>0){
          dp_zhi=jishu-1
          tp_zhi=jishu+deal_num
        }else{
          dp_zhi=jishu-deal_num
          tp_zhi=jishu+1
        }
        let dp_index = -1;
        let tp_index = -1;
        // 寻找最近跌破阈值的位置
        for (let i = data.length - 1; i >= 0; i--) {
            if (data[i] < dp_zhi) {
              dp_index = i;
                break;
            }
        }
        // 寻找最近突破阈值的位置
        for (let i = data.length - 1; i >= 0; i--) {
            if (data[i] > tp_zhi) {
              tp_index = i;
                break;
            }
        }

        return '上次突破'+tp_zhi+'时间'+all_time[tp_index]+'上次跌破'+dp_zhi+'时间'+all_time[dp_index];
    },
    getzdf_deal_data(data_time,item2){
      let that=this;
      let all_data= ( this.bug_shell_a_d_a.bug_shell_all_data_array[ this.timeAt(item2.end_time)]-
      this.bug_shell_a_d_a.bug_shell_all_data_array[  this.timeAt(item2.start_time)]).toFixed(2) 
       // 解析时间区间字符串
      const [startStr, endStr] = data_time.split(' 到 ');
      const startDate = new Date(startStr);
      const endDate = new Date(endStr);
      // 计算符合条件的交易数量总和
      let deal_data=  that.dealFlow.reduce((total, tx) => {
          const txDate = new Date(tx.time);
          return txDate >= startDate && txDate <= endDate ? total + tx.num : total;
      }, 0);
    },
    timeAt(time){
      let time_array = this.day_list.time
      return time_array.indexOf(time);
    },
    //搜索买入的时间  view_type=是不是要展示买入位置
    toSreach(time, view_type) {
      let length1 = this.day_list.inflow
      let time_array = this.day_list.time
      let percentage_array = 0
      let this_time2 = 0
      if (typeof time != "string") {
        this_time2 = time_array.findIndex((item) => {
          return item === time[time.length - 1].time + ""
        })
        this.$parent.dealLineData(this_time2);
        let end_data={
          name:null,
          num:0,
        }
        let length11=0
        if(this.bug_shell_a_d_a){
          length11=this.bug_shell_a_d_a.shell_cben_array.length-1
          percentage_array=this.bug_shell_a_d_a.bug_shell_all_data_array[length11] /
          this.bug_shell_a_d_a.shell_cben_array[length11]
          end_data= {
            name: "成本" + this.bug_shell_a_d_a.shell_cben_array[length11]+ "元",
            num: "盈利" +  this.bug_shell_a_d_a.day_loss_profit[length11]+ "元",
          }
        }
    
        this.sreach_data = [{
            name: "累加涨跌幅",
            num: (percentage_array*100).toFixed(2) + "%",
          },
          {
            name: "持有天数",
            num: length11+1
          },
          end_data
        ]
      } else {
        this_time2 = time_array.findIndex((item) => {
          return item === time + ""
        })
        percentage_array = (
          length1[length1.length - 1] - length1[this_time2]
        ).toFixed(2)

        this.$parent.dealLineData(this_time2, 2)

        this.sreach_data = [{
          name: "累加涨跌幅",
          num: percentage_array + "%",
        },
        {
          name: "持有天数",
          num: length1.length - this_time2,
        },
        ]
        if (view_type == 0) {
          // this.this_list_data =
           this.dealThisData(this.inflow, this_time2)

        }
      }
    },
    dealInflow(num, data) {
      let info = []
      let info_item = []
      data.slice(-num).forEach((element, index) => {
        if (element > 0) {
          info.push(element)
        } else {
          info_item.push(element)
        }
      })
      let all = data.slice(-num).reduce((a, b) => a + b)
      all = all / num
      info = info.length > 0 ? info.reduce((a, b) => a + b) / info.length : 0
      info_item = info_item.reduce((a, b) => a + b) / info_item.length
      return {
        all: {
          name: "平均值",
          num: all.toFixed(2),
        },
        z_all: {
          name: "正平均值",
          num: info.toFixed(2),
        },
        f_all: {
          name: "负平均值",
          num: info_item.toFixed(2),
        },
      }
    },
    dealThisData(data, this_num) {
      let day_list = this.day_list
      if (this_num) {
        this_num = this_num + 1
        day_list = {
          array: day_list.array.slice(0, this_num),
          inflow: day_list.inflow.slice(0, this_num),
          name: day_list.name,
          num: day_list.num.slice(0, this_num),
          time: day_list.time.slice(0, this_num),
        }
      }
      let info_array180 = dealFlowData(day_list, 180,1)
      let info_array40 = dealFlowData(day_list, 40,1)
      //20个工作日的涨跌幅
      let info_array20 = dealFlowData(day_list, 20,1)
      let info_array10 = dealFlowData(day_list, 10,1)


      this.day_list_data = [
        //20个工作日的涨跌幅
        // info_array40[2],
        info_array10[0],
        info_array10[1],
        info_array20[0],
        info_array20[1],
        info_array40[0],
        info_array40[1],
        info_array180[0],
        info_array180[1],
      ]
      this.day_list_data =cycleMax(this.day_list_data)
      this.this_list_data=getYearlyFluctuation(this.day_list)
      this.this_zdf_num=this.findLMax(this.day_list_data)

      let array20 = dealDayList(day_list, 20)
      let array10 = dealDayList(day_list, 10)
      let info_array2 = []
      day_list.array.slice(-40).forEach((item, index) => {
        let item1 = item > 0
        let item2 = day_list.array.slice(-40)[index - 1] > 0
        if (item2 == item1) {
          info_array2.push(item)
        } else {
          info_array2 = []
          info_array2.push(item)
        }
      })
      return [{
        name: "今天",
        num: data[1],
      },
      {
        name: "前一天",
        num: data[0],
      },
      {
        name: "累加涨跌幅" + info_array2.length + "天",
        num: info_array2
          .reduce((a, b) => parseFloat(a) + parseFloat(b))
          .toFixed(2),
      },
      array10[0],
      array20[0],
      array10[1],
      array20[1],
      ]
    },
  },
};
</script>
<style lang="scss" scoped>
.decrease {
  width: 100%;
  background-color: #343434;
  color: white;
  .box {
    display: flex;
  }
 .gird_box{
    margin-right: 5px;
    padding: 0px 5px;
 }
  .active1 {
    color: #f72c5b;
  }
  .active2 {
    color: #7cffb2;
  }
  
  ul {
    padding: 5px;
    margin-right: 20px;

    li {
      display: flex
;
    color: white;
    border: 1px solid #717171;
    border-top: 0px;
     }
      h4 {
        word-wrap: normal;
    word-break: keep-all;
    width: 100px;
    background: #ececec;
    color: #333;
    height: 40px;
    line-height: 40px;
    text-align: center;
    }
      h5 {
        font-weight: 600;
        word-break: keep-all;
        width: 60px;
      }
    h6{
      color: #ffffffa6;
    }
  

    .table{
        padding: 0px 5px;
        border-left: 1px solid #ffffff;
      }
      .active2 {
      color: #7cffb2a8;
    }
    .active1 {
      color: #f72c5b9c;
    }

  }

  .active3 h5 {
    font-size: 24px;
  }
}
</style>
